package com.inei.cenacom.model.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;

import com.inei.cenacom.model.entity.Cap200;
import com.inei.cenacom.model.entity.Cap300;
import com.inei.cenacom.model.entity.Cap400;

public class Cap400Dao extends ACRUD<Cap400> {

	public Cap400Dao(Context context) {
		super(context);
	}

	@Override
	public List<Cap400> leerTodo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Cap400> leer(String id, int anio) {
		List<Cap400> lista = null;
		db = util.getReadableDatabase();
		c = db.rawQuery("select * from CAP400 where ID_N=? and ANIO=?",
				new String[] { id, anio + "" });
		if (c.moveToFirst()) {
			lista = new ArrayList<Cap400>();
			while (!c.isAfterLast()) {
				Cap400 obj = new Cap400();
				obj.setID_N(id);
				obj.setANIO(anio);
				obj.setNRO_MREG(c.getInt(c.getColumnIndex("NRO_MREG")));
				obj.setNRO_PVREG(c.getInt(c.getColumnIndex("NRO_PVREG")));
				obj.setIVM401A(c.getString(c.getColumnIndex("IVM401A")));
				obj.setIVM401B(c.getString(c.getColumnIndex("IVM401B")));
				obj.setIVM401C(c.getString(c.getColumnIndex("IVM401C")));
				obj.setIVM402(c.getInt(c.getColumnIndex("IVM402")));
				obj.setIVH402_N(c.getString(c.getColumnIndex("IVH402_N")));
				obj.setIVM403(c.getInt(c.getColumnIndex("IVM403")));
				obj.setIVM404_DIA(c.getString(c.getColumnIndex("IVM404_DIA")));
				obj.setIVM404_MES(c.getString(c.getColumnIndex("IVM404_MES")));
				obj.setIVM404_ANIO(c.getString(c.getColumnIndex("IVM404_ANIO")));
				obj.setIVM405(c.getInt(c.getColumnIndex("IVM405")));
				obj.setIVM406(c.getInt(c.getColumnIndex("IVM406")));
				obj.setIVM406_O(c.getString(c.getColumnIndex("IVM406_O")));
				obj.setIVM407(c.getInt(c.getColumnIndex("IVM407")));
				obj.setIVM407_O(c.getString(c.getColumnIndex("IVM407_O")));
				obj.setIVM408(c.getInt(c.getColumnIndex("IVM408")));
				obj.setOBS_4(c.getString(c.getColumnIndex("OBS_4")));
				obj.setFEC_INI(c.getString(c.getColumnIndex("FEC_INI")));
				obj.setHOR_INI(c.getString(c.getColumnIndex("HOR_INI")));
				obj.setFEC_FIN(c.getString(c.getColumnIndex("FEC_FIN")));
				obj.setHOR_FIN(c.getString(c.getColumnIndex("HOR_FIN")));
				obj.setUSR_CREACION(c.getString(c
						.getColumnIndex("USR_CREACION")));
				obj.setFEC_CREACION(c.getString(c
						.getColumnIndex("FEC_CREACION")));
				obj.setUSR_EDICION(c.getString(c.getColumnIndex("USR_EDICION")));
				obj.setFEC_EDICION(c.getString(c.getColumnIndex("FEC_EDICION")));
				lista.add(obj);
				c.moveToNext();
			}
		}
		cerrar();
		return lista;
	}

	@Override
	public Cap400 buscar(Cap400 o) {
		Cap400 obj = null;
		db = util.getReadableDatabase();

		c = db.rawQuery(
				"select * from CAP400 where ID_N=? and ANIO=? and NRO_MREG=? and NRO_PVREG=?",
				new String[] { o.getID_N(), o.getANIO() + "",
						o.getNRO_MREG() + "", o.getNRO_PVREG() + "" });
		if (c.moveToFirst()) {
			obj = o;
			obj.setIVM401A(c.getString(c.getColumnIndex("IVM401A")));
			obj.setIVM401B(c.getString(c.getColumnIndex("IVM401B")));
			obj.setIVM401C(c.getString(c.getColumnIndex("IVM401C")));
			obj.setIVM402(c.getInt(c.getColumnIndex("IVM402")));
			obj.setIVH402_N(c.getString(c.getColumnIndex("IVH402_N")));
			obj.setIVM403(c.getInt(c.getColumnIndex("IVM403")));
			obj.setIVM404_DIA(c.getString(c.getColumnIndex("IVM404_DIA")));
			obj.setIVM404_MES(c.getString(c.getColumnIndex("IVM404_MES")));
			obj.setIVM404_ANIO(c.getString(c.getColumnIndex("IVM404_ANIO")));
			obj.setIVM405(c.getInt(c.getColumnIndex("IVM405")));
			obj.setIVM406(c.getInt(c.getColumnIndex("IVM406")));
			obj.setIVM406_O(c.getString(c.getColumnIndex("IVM406_O")));
			obj.setIVM407(c.getInt(c.getColumnIndex("IVM407")));
			obj.setIVM407_O(c.getString(c.getColumnIndex("IVM407_O")));
			obj.setIVM408(c.getInt(c.getColumnIndex("IVM408")));
			obj.setOBS_4(c.getString(c.getColumnIndex("OBS_4")));
			obj.setFEC_INI(c.getString(c.getColumnIndex("FEC_INI")));
			obj.setHOR_INI(c.getString(c.getColumnIndex("HOR_INI")));
			obj.setFEC_FIN(c.getString(c.getColumnIndex("FEC_FIN")));
			obj.setHOR_FIN(c.getString(c.getColumnIndex("HOR_FIN")));
			obj.setUSR_CREACION(c.getString(c.getColumnIndex("USR_CREACION")));
			obj.setFEC_CREACION(c.getString(c.getColumnIndex("FEC_CREACION")));
			obj.setUSR_EDICION(c.getString(c.getColumnIndex("USR_EDICION")));
			obj.setFEC_EDICION(c.getString(c.getColumnIndex("FEC_EDICION")));
		}

		cerrar();
		return obj;
	}

	@Override
	public int crear(Cap400 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		cv.put("ID_N", o.getID_N());
		cv.put("ANIO", o.getANIO());
		cv.put("NRO_MREG", o.getNRO_MREG());
		cv.put("NRO_PVREG", o.getNRO_PVREG());
		cv.put("IVM401A", o.getIVM401A());
		cv.put("IVM401B", o.getIVM401B());
		cv.put("IVM401C", o.getIVM401C());
		cv.put("IVM402", o.getIVM402());
		cv.put("IVH402_N", o.getIVH402_N());
		cv.put("IVM403", o.getIVM403());
		cv.put("IVM404_DIA", o.getIVM404_DIA());
		cv.put("IVM404_MES", o.getIVM404_MES());
		cv.put("IVM404_ANIO", o.getIVM404_ANIO());
		cv.put("IVM405", o.getIVM405());
		cv.put("IVM406", o.getIVM406());
		cv.put("IVM406_O", o.getIVM406_O());
		cv.put("IVM407", o.getIVM407());
		cv.put("IVM407_O", o.getIVM407_O());
		cv.put("IVM408", o.getIVM408());
		cv.put("OBS_4", o.getOBS_4());
		cv.put("FEC_INI", o.getFEC_INI());
		cv.put("HOR_INI", o.getHOR_INI());
		cv.put("FEC_FIN", o.getFEC_FIN());
		cv.put("HOR_FIN", o.getHOR_FIN());
		String date = "";
		while (date.equals("")) {
			date = obtenerDate();
		}
		cv.put("USR_CREACION", o.getUSR_CREACION());
		cv.put("FEC_CREACION", date);
		cv.put("USR_EDICION", o.getUSR_EDICION());
		cv.put("FEC_EDICION", date);

		resultado = (int) db.insert("CAP400", null, cv);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		cerrar();
		return resultado;
	}

	@Override
	public int actualizar(Cap400 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		cv.put("IVM401A", o.getIVM401A());
		cv.put("IVM401B", o.getIVM401B());
		cv.put("IVM401C", o.getIVM401C());
		cv.put("IVM402", o.getIVM402());
		cv.put("IVH402_N", o.getIVH402_N());
		cv.put("IVM403", o.getIVM403());
		cv.put("IVM404_DIA", o.getIVM404_DIA());
		cv.put("IVM404_MES", o.getIVM404_MES());
		cv.put("IVM404_ANIO", o.getIVM404_ANIO());
		cv.put("IVM405", o.getIVM405());
		cv.put("IVM406", o.getIVM406());
		cv.put("IVM406_O", o.getIVM406_O());
		cv.put("IVM407", o.getIVM407());
		cv.put("IVM407_O", o.getIVM407_O());
		cv.put("IVM408", o.getIVM408());
		cv.put("OBS_4", o.getOBS_4());
		cv.put("FEC_INI", o.getFEC_INI());
		cv.put("HOR_INI", o.getHOR_INI());
		cv.put("FEC_FIN", o.getFEC_FIN());
		cv.put("HOR_FIN", o.getHOR_FIN());

		String date = "";
		while (date.equals("")) {
			date = obtenerDate();
		}
		if (o.getFEC_CREACION() == null) {
			cv.put("FEC_CREACION", date);
		} else if (o.getFEC_CREACION().equals("")) {
			cv.put("FEC_CREACION", date);
		}
		cv.put("USR_EDICION", o.getUSR_EDICION());
		cv.put("FEC_EDICION", date);

		resultado = db.update("CAP400", cv,
				"ID_N=? and ANIO=? and NRO_MREG=? and NRO_PVREG=?",
				new String[] { o.getID_N(), o.getANIO() + "",
						o.getNRO_MREG() + "", o.getNRO_PVREG() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		cerrar();
		return resultado;
	}

	public String obtenerDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(Calendar.getInstance().getTime());
	}

	@Override
	public int eliminar(Cap400 o) {
		db = util.getReadableDatabase();
		Cap200 c200 = new Cap200();
		c = db.rawQuery(
				"Select IH213 from CAP200 where ID_N=? AND ANIO=? AND NRO_MREG=?",
				new String[] { o.getID_N(), o.getANIO() + "",
						o.getNRO_MREG() + "" });
		if (c.moveToFirst()) {
			c200.setID_N(o.getID_N());
			c200.setANIO(o.getANIO());
			c200.setNRO_MREG(o.getNRO_MREG());
			c200.setIH213(c.getInt(0));
		}
		cerrar();
		try {
			db = util.getWritableDatabase();
			int res = db.delete(
					"CAP400",
					"ID_N=? AND ANIO=? AND NRO_MREG=? AND NRO_PVREG=?",
					new String[] { o.getID_N(), o.getANIO() + "",
							o.getNRO_MREG() + "", o.getNRO_PVREG() + "" });
			if (res != 0) {

				cv = new ContentValues();
				cv.put("IH213", c200.getIH213() - 1);
				res = db.update(
						"CAP200",
						cv,
						"ID_N=? AND ANIO=? AND NRO_MREG=?",
						new String[] { o.getID_N(), o.getANIO() + "",
								o.getNRO_MREG() + "" });
				if (res == 0) {
					throw new Exception();
				}
				cerrar();
				List<Cap400> l400 = leer(o.getID_N(), o.getANIO(),
						o.getNRO_MREG());
				db = util.getWritableDatabase();
				if (l400 != null) {
					for (int i = 1; i <= l400.size(); i++) {
						Cap400 c400 = l400.get(i - 1);
						cv = new ContentValues();
						cv.put("NRO_VFREG", i);
						res = db.update(
								"CAP400",
								cv,
								"ID_N=? AND ANIO=? AND NRO_MREG=? AND NRO_PVREG=?",
								new String[] { c400.getID_N(),
										c400.getANIO() + "",
										c400.getNRO_MREG() + "",
										c400.getNRO_PVREG() + "" });
						if (res == 0) {
							throw new Exception();
						}
					}
				}
			} else {
				throw new Exception();
			}

			cerrar();

			return 1;
		} catch (Exception e) {

			cerrar();

			return 0;
		}

	}

	// otros metodos
	public List<Cap400> leer(String id, int anio, int NRO_MREG) {
		List<Cap400> lista = null;
		db = util.getReadableDatabase();
		c = db.rawQuery(
				"select * from CAP400 where ID_N=? and ANIO=? and NRO_MREG=?",
				new String[] { id + "", anio + "", NRO_MREG + "" });
		if (c.moveToFirst()) {
			lista = new ArrayList<Cap400>();
			while (!c.isAfterLast()) {
				Cap400 obj = new Cap400();
				obj.setID_N(id);
				obj.setANIO(anio);
				obj.setNRO_MREG(NRO_MREG);
				obj.setNRO_PVREG(c.getInt(c.getColumnIndex("NRO_PVREG")));
				obj.setIVM401A(c.getString(c.getColumnIndex("IVM401A")));
				obj.setIVM401B(c.getString(c.getColumnIndex("IVM401B")));
				obj.setIVM401C(c.getString(c.getColumnIndex("IVM401C")));
				obj.setIVM402(c.getInt(c.getColumnIndex("IVM402")));
				obj.setIVH402_N(c.getString(c.getColumnIndex("IVH402_N")));
				obj.setIVM403(c.getInt(c.getColumnIndex("IVM403")));
				obj.setIVM404_DIA(c.getString(c.getColumnIndex("IVM404_DIA")));
				obj.setIVM404_MES(c.getString(c.getColumnIndex("IVM404_MES")));
				obj.setIVM404_ANIO(c.getString(c.getColumnIndex("IVM404_ANIO")));
				obj.setIVM405(c.getInt(c.getColumnIndex("IVM405")));
				obj.setIVM406(c.getInt(c.getColumnIndex("IVM406")));
				obj.setIVM406_O(c.getString(c.getColumnIndex("IVM406_O")));
				obj.setIVM407(c.getInt(c.getColumnIndex("IVM407")));
				obj.setIVM407_O(c.getString(c.getColumnIndex("IVM407_O")));
				obj.setIVM408(c.getInt(c.getColumnIndex("IVM408")));
				obj.setOBS_4(c.getString(c.getColumnIndex("OBS_4")));
				obj.setFEC_INI(c.getString(c.getColumnIndex("FEC_INI")));
				obj.setHOR_INI(c.getString(c.getColumnIndex("HOR_INI")));
				obj.setFEC_FIN(c.getString(c.getColumnIndex("FEC_FIN")));
				obj.setHOR_FIN(c.getString(c.getColumnIndex("HOR_FIN")));
				obj.setUSR_CREACION(c.getString(c
						.getColumnIndex("USR_CREACION")));
				obj.setFEC_CREACION(c.getString(c
						.getColumnIndex("FEC_CREACION")));
				obj.setUSR_EDICION(c.getString(c.getColumnIndex("USR_EDICION")));
				obj.setFEC_EDICION(c.getString(c.getColumnIndex("FEC_EDICION")));
				lista.add(obj);
				c.moveToNext();
			}
		}
		cerrar();
		return lista;
	}
}
